package com.example.myapplication.okhttp;

import android.util.Log;

import com.google.gson.JsonArray;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class HttpUtil {
    public static JSONArray getResult(String content) throws IOException, JSONException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        Request request = new Request.Builder()
                .url("https://pigskin.cn.utools.club/"+content)
                .method("GET", null)
                .addHeader("Cookie", "JSESSIONID=6F94949F53CB36D675264117DA938359")
                .build();
        Response response = client.newCall(request).execute();
        JSONArray jsonArray = new JSONArray(response.body().string());
        return jsonArray;
    }

    public static JSONObject getUser(String content) throws IOException, JSONException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        Request request = new Request.Builder()
                .url("https://pigskin.cn.utools.club/"+content)
                .method("GET", null)
                .addHeader("Cookie", "JSESSIONID=6F94949F53CB36D675264117DA938359")
                .build();
        Response response = client.newCall(request).execute();
        JSONObject jsonArray = new JSONObject(response.body().string());
        return jsonArray;
    }

    public static void send(String content) throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = RequestBody.create(mediaType, "");
        Request request = new Request.Builder()
                .url("https://pigskin.cn.utools.club/")
                .method("POST", body)
                .build();
        Response response = client.newCall(request).execute();
    }
    public static JSONObject getWeather(String content) throws IOException, JSONException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        Request request = new Request.Builder()
                .url("http://apis.juhe.cn/simpleWeather/query?city="+content+"&key=2d1c596ef98cb71b9bd836b552f61741")
                .method("GET", null)
                .addHeader("Cookie", "JSESSIONID=6F94949F53CB36D675264117DA938359")
                .build();
        Response response = client.newCall(request).execute();
        JSONObject jsonArray = new JSONObject(response.body().string());
        return jsonArray;
    }
}
